package com.kty.sdk.okhttp.request;

import java.util.Map;

import okhttp3.FormBody;
import okhttp3.Request;

/**
 * TODO:接收请求参数 生成request对象
 * Created by 倪彬彬 on 2017/11/27.
 */

public class CommonRequest {

    /**
     * TODO:返回一个request 对象
     *
     * @param url
     * @param params
     * @return 返回一个post 类型的equest 对象
     * @author 倪彬彬
     */
    public static Request createPostRequest(String url, RequestParams params) {
        FormBody.Builder mFormBodyBuilder = new FormBody.Builder();

        if (params != null) {
            for (Map.Entry<String, String> entry : params.urlParams.entrySet()) {
                mFormBodyBuilder.add(entry.getKey(), entry.getValue());
            }
        }
        FormBody mFromBody = mFormBodyBuilder.build();

        return new Request.Builder().url(url).post(mFromBody).build();
    }

    /**
     * @param url
     * @param params
     * @return 返回一个 get类型的请求
     */
    public static Request createGetRequest(String url, RequestParams params) {
        StringBuilder urlBulidler = new StringBuilder(url).append("?");
        if (params != null) {
            for (Map.Entry<String, String> entry : params.urlParams.entrySet()) {
                urlBulidler.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }

        return new Request.Builder().url(urlBulidler.substring(0, urlBulidler.length() - 1)).get().build();
    }
}
